package com.a0soft.gphone.aTruffleHog.wnd.tablet;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.analytic.AnalyticsUtil;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseMultiPaneActivity;
import com.a0soft.gphone.aTruffleHog.scrsht.Screenshot;
import com.a0soft.gphone.aTruffleHog.util.aDebug;
import com.a0soft.gphone.aTruffleHog.wnd.stat.BaseViewPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.DefaultStatsPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.FindDevicesByCPUFormFrg;
import com.a0soft.gphone.aTruffleHog.wnd.stat.FindDevicesByCPUPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.FindDevicesByCameraFormFrg;
import com.a0soft.gphone.aTruffleHog.wnd.stat.FindDevicesByCameraPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.OsStatsPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.RootedStatsPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.StatsListFrg;
import com.a0soft.gphone.aTruffleHog.wnd.stat.TopDeviceStatsPager;
import com.a0soft.gphone.aTruffleHog.wnd.stat.TopOperatorStatsPager;
import com.jakewharton.android.viewpagerindicator.TitlePageIndicator;

public class StatsWnd extends BaseMultiPaneActivity {
    private BaseViewPager m_pager;
    
    private ViewPager           m_vPager;
    private TitlePageIndicator  m_vIndicator;
    
    private static final String LOG_TAG = StatsWnd.class.getSimpleName();
    
    ////////////////////////////////////////
    @Override protected void 
    onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        if (!DeviceInfo.GetInstance(this).IsReady()) {
            finish();
            return;
        }
        
        setContentView(R.layout.stats_wnd);
        
        m_vPager = (ViewPager)findViewById(R.id.pager);
        m_vIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
    }
    
    @Override protected void 
    onPostCreate(Bundle savedInstanceState) 
    {
        super.onPostCreate(savedInstanceState);
        getActivityHelper().setupSubActivity();
        
        AnalyticsUtil.GetInstance(this).TrackPageView(AnalyticsUtil.PAGE_STATISTICS);
    }
    
    @Override public void 
    onBackPressed()
    {
        if (m_pager != null) {
            if (m_pager instanceof FindDevicesByCameraPager) {
                if (((FindDevicesByCameraPager)m_pager).BackToFirstPage())
                    return;
            }
            else if (m_pager instanceof FindDevicesByCPUPager) {
                if (((FindDevicesByCPUPager)m_pager).BackToFirstPage())
                    return;
            }
        }
        super.onBackPressed();
    }
    
    @Override public void 
    onNotifyByFragment(Class<?> clsFrg, Intent it)
    {
        if (aDebug.ENABLED) {
            aDebug.d(LOG_TAG, "onNotifyByFragment()");
        }
        if (it == null) return;
        
        if (clsFrg.equals(StatsListFrg.class)) {        
            int nSelItemPos = it.getIntExtra(StatsListFrg.EXTRA_SEL_ITEM_POS, Integer.MIN_VALUE);
            boolean bSelItemChanged = it.getBooleanExtra(StatsListFrg.EXTRA_SEL_ITEM_CHANGED, false);    
            boolean bSelChangedManually = it.getBooleanExtra(StatsListFrg.EXTRA_SEL_CHANGED_MANUALLY, true);    
            if (aDebug.ENABLED) {
                aDebug.d(LOG_TAG, "  nSelItemPos=" + nSelItemPos);
                aDebug.d(LOG_TAG, "  bSelItemChanged=" + bSelItemChanged);
                aDebug.d(LOG_TAG, "  bSelChangedManually=" + bSelChangedManually);
            }
            
            boolean bChanged = false;
            switch (nSelItemPos) {
            case StatsListFrg.ITEM_OS_STATS:
                if (m_pager == null || !(m_pager instanceof OsStatsPager)) {
                    m_pager = new OsStatsPager();
                    bChanged = true;
                }
                break;
            case StatsListFrg.ITEM_ROOTED_STATS:
                if (m_pager == null || !(m_pager instanceof RootedStatsPager)) {
                    m_pager = new RootedStatsPager();
                    bChanged = true;
                }
                break;
            case StatsListFrg.ITEM_TOP_DEVICE_STATS:
                if (m_pager == null || !(m_pager instanceof TopDeviceStatsPager)) {
                    m_pager = new TopDeviceStatsPager();
                    bChanged = true;
                }
                break;
            case StatsListFrg.ITEM_TOP_OPERATOR_STATS:
                if (m_pager == null || !(m_pager instanceof TopOperatorStatsPager)) {
                    m_pager = new TopOperatorStatsPager();
                    bChanged = true;
                }
                break;
            case StatsListFrg.ITEM_FIND_BY_CAMERA:
                if (m_pager == null || !(m_pager instanceof FindDevicesByCameraPager)) {
                    m_pager = new FindDevicesByCameraPager();
                    bChanged = true;
                }
                break;
            case StatsListFrg.ITEM_FIND_BY_CPU:
                if (m_pager == null || !(m_pager instanceof FindDevicesByCPUPager)) {
                    m_pager = new FindDevicesByCPUPager();
                    bChanged = true;
                }
                break;
            default:
                if (m_pager == null || !(m_pager instanceof DefaultStatsPager)) {
                    m_pager = new DefaultStatsPager();
                    bChanged = true;
                }
                break;
            }
            
            if (bChanged) {
                m_pager.SetupViews(this, m_vPager, m_vIndicator);
            }
        }
        else if (clsFrg.equals(FindDevicesByCameraFormFrg.class)) {
            int nMinPixels = it.getIntExtra(FindDevicesByCameraFormFrg.EXTRA_CAMERA_PIXELS_MIN, -1);
            int nMaxPixels = it.getIntExtra(FindDevicesByCameraFormFrg.EXTRA_CAMERA_PIXELS_MAX, -1);
            if (aDebug.ENABLED) {
                aDebug.d(LOG_TAG, "  nMinPixels=" + nMinPixels + ", nMaxPixels=" + nMaxPixels);
            }    
            
            if (m_pager != null && m_pager instanceof FindDevicesByCameraPager) {
                ((FindDevicesByCameraPager)m_pager).FindDevices(this, nMinPixels, nMaxPixels);
            }
        }
        else if (clsFrg.equals(FindDevicesByCPUFormFrg.class)) {
            int nMinCoreNum = it.getIntExtra(FindDevicesByCPUFormFrg.EXTRA_CORE_NUM_MIN, -1);
            int nMaxCoreNum = it.getIntExtra(FindDevicesByCPUFormFrg.EXTRA_CORE_NUM_MAX, -1);
            int nMinCpuSpeed = it.getIntExtra(FindDevicesByCPUFormFrg.EXTRA_CPU_SPEED_MIN, -1);
            int nMaxCpuSpeed = it.getIntExtra(FindDevicesByCPUFormFrg.EXTRA_CPU_SPEED_MAX, -1);
            if (aDebug.ENABLED) {
                aDebug.d(LOG_TAG, "  nMinCoreNum=" + nMinCoreNum + ", nMaxCoreNum=" + nMaxCoreNum);
                aDebug.d(LOG_TAG, "  nMinCpuSpeed=" + nMinCpuSpeed + ", nMaxCpuSpeed=" + nMaxCpuSpeed);
            }    
            
            if (m_pager != null && m_pager instanceof FindDevicesByCPUPager) {
                ((FindDevicesByCPUPager)m_pager).FindDevices(this, 
                                                             nMinCoreNum, nMaxCoreNum, 
                                                             nMinCpuSpeed, nMaxCpuSpeed);
            }
        }
    }
    
    @Override public boolean
    onCreateOptionsMenu(Menu menu) 
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onCreateOptionsMenu()");
        getMenuInflater().inflate(R.menu.stats_wnd, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override public boolean 
    onOptionsItemSelected(MenuItem item)
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onOptionsItemSelected()");
        final int ID = item.getItemId();
        if (ID == R.id.menu_screenshot) {
            Screenshot.TakeScreenshot(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
